<?php
if(!defined('IN_DISCUZ')) {exit('Access Denied');}
define('IN_ADMINCP', TRUE);
define('NOROBOT', TRUE);
define('ADMINSCRIPT', basename(__FILE__));
define('CURSCRIPT', 'admin');

//require_once (DISCUZ_ROOT.'./include/common.inc.php');
//require DISCUZ_ROOT.'./forumdata/cache/plugin_black_mmm.php';
$operation=ban;
$logdir = DISCUZ_ROOT.'./data/log/';
$logfiles = get_log_files($logdir, $operation.'log');
if(empty($day)){$max=count($logfiles);$max=$max-1;$day=substr($logfiles[$max],0,6);}
	$sel = '<select class="right" onchange="location.href=\''.$BASESCRIPT.'?id=black_mmm:index'.'&day=\'+this.value">';
	foreach($logfiles as $logfile) {
		list($date) = explode('_', $logfile);
		$sel .= '<option value="'.$date.'"'.($date == $day ? ' selected="selected"' : '').'>'.$date.'</option>';
	}
	$sel .= '</select>';
@$logs = file(!empty($day) ? $logdir.$day.'_'.$operation.'log.php' : $logdir.$logfiles[0]);
if(empty($logs)){showmessage("black_mmm:error");}
$logs = array_reverse($logs);
if(empty($keyword)) {
	$num = count($logs);
	$multipage = multi($num, $lpp, $page, "$BASESCRIPT?action=logs&operation=$operation&lpp=$lpp".(!empty($day) ? '&day='.$day : ''), 0, 3);
	$logs = array_slice($logs, $start, $lpp);

} else {
	foreach($logs as $key => $value) {
		if(strpos($value, $keyword) === FALSE) {
			unset($logs[$key]);
		}
	}
	$multipage = '';
}
if($operation == 'ban') {

	/*showtablerow('class="header"', array('class="td23"', 'class="td23"', 'class="td23"', 'class="td23"', 'class="td23"', 'class="td23"', 'class="td24"', 'class="td23"'), array(
		lang('operator'),
		lang('usergroup'),
		lang('ip'),
		lang('time'),
		lang('username'),
		lang('operation'),
		lang('logs_banned_group'),
		lang('validity'),
		lang('reason'),
	));*/
	$query = DB::query("SELECT groupid, grouptitle FROM ".DB::table('common_usergroup')."");
	while($group = DB::fetch($query)) {$usergroup[$group['groupid']] = $group['grouptitle'];}
	
	foreach($logs as $logrow) {
	    $i=1;$i++;
		$log = explode("\t", $logrow);
		if(empty($log[1])) {
			continue;
		}
		$log[1] = gmdate('y-n-j H:i', $log[1] + $timeoffset * 3600);
		$log[2] = "<a href=\"space.php?username=".rawurlencode($log[2])."\" target=\"_blank\">$log[2]";
		$log[3] = $usergroup[$log[3]];
		$log[4] = $allowviewip ? $log[4] : '-';
		$log[5] = "<a href=\"space.php?username=".rawurlencode($log[5])."\" target=\"_blank\">$log[5]</a>";
		$log[8] = trim($log[8]) ? gmdate('y-n-j', $log[8] + $timeoffset * 3600) : '';
		$log[9] = $log[9];
		$loop[] = array('1'=>$log[2],'2'=>$log[3],'3'=>$log[4],'4'=>$log[1],'5'=>$log[5],'6'=>(in_array($log[6], array(4, 5)) && !in_array($log[7], array(4, 5)) ? '<i>'.$scriptlang['black_mmm']['jie'].'</i>' : '<b>'.$scriptlang['black_mmm']['jin'].'</b>'),'7'=>"{$usergroup[$log[6]]}/{$usergroup[$log[7]]}",'8'=>$log[8],'9'=>$log[9]);
		/*showtablerow('', array('class="bold"'), array(
			$log[2],
			$log[3],
			$log[4],
			$log[1],
			$log[5],
			(in_array($log[6], array(4, 5)) && !in_array($log[7], array(4, 5)) ? '<i>'.$lang['logs_banned_unban'].'</i>' : '<b>'.$lang['logs_banned_ban'].'</b>'),
			"{$usergroup[$log[6]]} / {$usergroup[$log[7]]}",
			$log[8],
			$log[9],
		));*/
	}

} 


function get_log_files($logdir = '', $action = 'action') {
	$dir = opendir($logdir);
	$files = array();
	while($entry = readdir($dir)) {
		$files[] = $entry;
	}
	closedir($dir);

	if($files) {
		sort($files);
		$logfile = $action;
		$logfiles = array();
		$ym = '';
		foreach($files as $file) {
			if(strpos($file, $logfile) !== FALSE) {
				if(substr($file, 0, 6) != $ym) {
					$ym = substr($file, 0, 6);
				}
				$logfiles[$ym][] = $file;
			}
		}
		if($logfiles) {
			$lfs = array();
			foreach($logfiles as $ym => $lf) {
				$lastlogfile = $lf[0];
				unset($lf[0]);
				$lf[] = $lastlogfile;
				$lfs = array_merge($lfs, $lf);
			}
			return array_slice($lfs, -2, 2);
		}
		return array();
	}
	return array();
}

include template('black_mmm:index');
?>